{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9fc45665",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f28513ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([7.3891])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.exp(torch.tensor([2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "50ad78fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.71828183])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(np.array([1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "59eab227",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.array([4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a1dd6401",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(np.e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67c91eec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.0108,  0.3991,  0.0140,  0.6516, -0.0486],\n",
       "        [ 0.7267, -0.6757,  0.7173, -0.7379, -1.1352],\n",
       "        [ 0.3529, -1.5655,  0.9626,  1.5225,  1.0139],\n",
       "        [-0.7304, -1.7970, -1.3821,  0.8187, -1.1846],\n",
       "        [-1.7143,  0.8335, -0.7307,  0.0771,  1.7404]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d21bf665",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0., 0., 0.],\n",
       "        [1., 1., 0., 0., 0.],\n",
       "        [1., 1., 1., 0., 0.],\n",
       "        [1., 1., 1., 1., 0.],\n",
       "        [1., 1., 1., 1., 1.]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1.]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = torch.ones(5,5)\n",
    "y=torch.triu(x,diagonal=1)\n",
    "display(1-y,x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d03ec86d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 1., 1., 1., 1.],\n",
       "        [0., 0., 1., 1., 1.],\n",
       "        [0., 0., 0., 1., 1.],\n",
       "        [0., 0., 0., 0., 1.],\n",
       "        [0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(x,diagonal=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e86952ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\caofei\\AppData\\Local\\Temp\\ipykernel_19448\\4234339829.py:5: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.\n",
      "  F.softmax(x.masked_fill(y==0,-1e9))\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[0.0020, 0.0020, 0.0020,  ..., 0.0020, 0.0020, 0.0020],\n",
       "        [0.0020, 0.0020, 0.0020,  ..., 0.0020, 0.0020, 0.0020],\n",
       "        [0.0020, 0.0020, 0.0020,  ..., 0.0020, 0.0020, 0.0020],\n",
       "        [0.0020, 0.0020, 0.0020,  ..., 0.0020, 0.0020, 0.0020],\n",
       "        [0.0020, 0.0020, 0.0020,  ..., 0.0020, 0.0020, 0.0020]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# y=torch.ones(5,512)\n",
    "# y=torch.triu(y,diagonal=0)\n",
    "x=torch.rand(5,512)\n",
    "y=torch.zeros(5,512)\n",
    "F.softmax(x.masked_fill(y==0,-1e9))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "19db77d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1000000000.0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-1e9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b61fc7d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1e-09"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1e-9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "77b11b1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.ones(9).reshape(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f1036044",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 1., 1.],\n",
       "       [0., 0., 1.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.triu(a,k=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "3abeb1b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch.nn as nn\n",
    "import torch\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "d94d530f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "cf91430c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.4013e-45, 2.6894e-01, 7.3106e-01],\n",
       "        [9.0031e-02, 2.4473e-01, 6.6524e-01]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.array([[-100,2,3],[4,5,6]])\n",
    "a=torch.from_numpy(a).float()\n",
    "F.softmax(a,dim=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "d31aa711",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0.],\n",
       "        [1., 1., 0.],\n",
       "        [1., 1., 1.]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1-torch.triu(torch.ones(3,3),diagonal=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "2999e12f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.2433,  0.6476, -0.7779, -1.9020, -0.9475],\n",
       "        [-0.1725, -0.8907, -2.0766, -0.3741, -1.1532],\n",
       "        [-0.0846,  0.7686, -0.4783, -1.6124,  1.1992]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(3, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "74bb0d82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.1965, -3.3590, -2.8749, -3.0809, -3.1869],\n",
       "        [-2.3897, -1.4167, -3.1458, -0.7368, -1.9370],\n",
       "        [-2.1509, -1.4128, -1.1958, -1.5816, -2.0245]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nn.LogSoftmax(dim=1)(torch.randn(3, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edb97dc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "file_bytes = open('demo.pdf', 'rb').read() \n",
    "res = requests.post(url='https://ai-model.chint.com/api/upload-file',\n",
    "                headers={'Authorization': 'Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'},\n",
    "                files={'file': ('demo.pdf', file_bytes, 'application/pdf')},\n",
    "                data={'model': 'MinerU'}\n",
    "                )\n",
    "if res.status_code == 200:\n",
    "    content = res.json()\n",
    "    print(content)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
